<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Merchandising extends CI_Controller
{

    function merchandising()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->load->model('mmaster');
        $this->load->model('mservices');
        $this->load->model('mterritory');
        $this->load->model('mpage');
        //$this->load->model('msales');
        $this->load->model('mmerchan');
        $this->is_logged_in();
        #$this->mjadwal->saveLog();
        $this->load->helper('url');
    }

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if (!isset($is_logged_in) || $is_logged_in != true) {
            redirect(base_url() . 'index.php/login');
        }
    }


    function index()
    {
        $this->create();
    }

    function create($proses = "add", $id = "")
    {
        $template = "shell/smooth";
        $data['main_view'] = "grid-merchandising";
        $data['jenis'] = "view";
        $data['proses'] = $proses;
        $data['id'] = $id;
        $data['region'] = $this->mmerchan->getRegion();
        $data['userGroupId'] = $this->mmerchan->getUserGroupId($this->session->userdata('username'));

        $this->load->view($template, $data, false);
    }
    
    
    
    function load_cluster($region_id){
        // process posted form data (the requested items like province)
        $data['test'] = $this->mmerchan->getCluster($region_id); //Search DB
        
        if('IS_AJAX')
        {
            echo json_encode($data['test']);
        }
    }
    
    function load_merchan($cluster_id = null, $offset = 0){
        // process posted form data (the requested items like province)
        /**$data['test'] = $this->mmerchan->getMerchan($cluster_id); //Search DB
        
        if('IS_AJAX')
        {
            echo json_encode($data['test']);
        }*/
        
        $data['test'] = $this->mmerchan->getMerchan($cluster_id, 15, $offset); //Search DB
        
        if($data['test'] == 'false')
        {
                
        }
        else
        {
            if('IS_AJAX')
            {
                echo json_encode($data['test']);
            }    
        }
    }
    
    function total_merchan($cluster_id = null){
        // process posted form data (the requested items like province)
        /**$data['test'] = $this->mmerchan->getMerchan($cluster_id); //Search DB
        
        if('IS_AJAX')
        {
            echo json_encode($data['test']);
        }*/
        
        $data['test'] = $this->mmerchan->totalMerchan($cluster_id); //Search DB
        
        if($data['test'] == 'false')
        {
                
        }
        else
        {
            if('IS_AJAX')
            {
                echo json_encode($data['test']);
            }    
        }
    }
    
    public function read($image_type)
	{
	    //echo $_GET['iDisplayStart'];echo 'halooo';return;
		$result = $this->mmerchan->getMerchants($image_type);
        $start=$_GET['iDisplayStart'];
        $result['iDisplayStart']=$start;
		echo json_encode($result);
	}
    
    public function read2($clusterID)
	{
		$result = $this->mmerchan->getMerchants2($clusterID);
		echo json_encode($result);
	}
    
    
}
